package com.zy.explore.rocketmq.interfaces.facade.example;

import com.zy.explore.rocketmq.interfaces.vo.ResultVO;
import com.zy.explore.rocketmq.application.service.OssService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * description OSS示例Facade
 *
 * @author 🇨🇳中国印🇨🇳
 * @date 2021年05月15日 14:27
 */
@Api(tags = {"OSS示例Facade"})
@RestController
@RequestMapping("/oss")
public class AliOssExampleFacade {

    @Resource
    private OssService ossService;

    /**
     * 上传MultipartFile返回下载地址
     * @param file 上传的文件
     * @return 返回下载地址
     * @throws IOException IO异常
     */
    @ApiOperationSupport(order = 10)
    @ApiOperation("上传MultipartFile至OSS返回下载地址")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType="__file", name = "file", value = "文件")
    })
    @PostMapping(value = "/uploadMultipartFile", headers = "content-type=multipart/form-data")
    public ResultVO<String> uploadMultipartFile(@RequestPart("file") MultipartFile file) throws IOException {
        String downloadUrl = ossService.uploadFile(file);
        return ResultVO.getSuccess(downloadUrl);
    }

    /**
     * 上传MultipartFile返回下载地址
     * @param file 上传的文件
     * @return 返回下载地址
     * @throws IOException IO异常
     */
    @ApiOperationSupport(order = 20)
    @ApiOperation("上传MultipartFile至OSS返回下载地址(流式上传)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType="__file", name = "file", value = "文件")
    })
    @PostMapping(value = "/uploadMultipartFileByBytes", headers = "content-type=multipart/form-data")
    public ResultVO<String> uploadMultipartFileByBytes(@RequestPart("file") MultipartFile file) throws IOException {
        String downloadUrl = ossService.uploadFile(file.getBytes(), file.getOriginalFilename(), file.getContentType(), file.getOriginalFilename());
        return ResultVO.getSuccess(downloadUrl);
    }

}
